#include "sdk.h"
#include "cpuregistersdlg.h"
#include <wx/intl.h>
#include <wx/sizer.h>
#include <wx/listctrl.h>

BEGIN_EVENT_TABLE( CPURegistersDlg, wxPanel )
END_EVENT_TABLE()

CPURegistersDlg::CPURegistersDlg( wxWindow* parent ) :
  wxPanel( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize ) {
  wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
  m_pList = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL );
  sizer->Add( m_pList, 1, wxGROW );
  SetSizer( sizer );
  Layout();
  wxFont font( 8, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL );
  m_pList->SetFont( font );
  Clear();
}

void CPURegistersDlg::Clear() {
  m_pList->ClearAll();
  m_pList->Freeze();
  m_pList->DeleteAllItems();
  m_pList->InsertColumn( 0, _T( "Register" ), wxLIST_FORMAT_LEFT );
  m_pList->InsertColumn( 1, _T( "Hex" ), wxLIST_FORMAT_RIGHT );
  m_pList->InsertColumn( 2, _T( "Interpreted" ), wxLIST_FORMAT_LEFT );
  m_pList->Thaw();
}

int CPURegistersDlg::RegisterIndex( const wxString& reg_name ) {
  for( int i = 0; i < m_pList->GetItemCount(); ++i ) {
    if( m_pList->GetItemText( i ).CmpNoCase( reg_name ) == 0 ) {
      return i;
    }
  }
  return -1;
}

void CPURegistersDlg::SetRegisterValue( const wxString& reg_name, const wxString& hexValue, const wxString& interpreted ) {
  int idx = RegisterIndex( reg_name );
  if( idx == -1 ) {
    idx = m_pList->GetItemCount();
    m_pList->InsertItem( idx, reg_name );
  }
  m_pList->SetItem( idx, 1, hexValue );
  m_pList->SetItem( idx, 2, interpreted );
  const int autoSizeMode = wxLIST_AUTOSIZE;
  for( int i = 0; i < 3; ++i ) {
    m_pList->SetColumnWidth( i, autoSizeMode );
  }
}

void CPURegistersDlg::EnableWindow( bool enable ) {
  m_pList->Enable( enable );
}
